package ${classInfo.basePackage}.service.impl;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.quancheng.common.basic.rest.*;
import com.quancheng.starter.log.LogUtil;
import com.quancheng.starter.log.QcLog;
import ${classInfo.basePackage}.service.${classInfo.className}Service;
import ${classInfo.basePackage}.dao.${classInfo.className}Mapper;
import ${classInfo.basePackage}.dataobject.${classInfo.className}DO;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;

/**
 * ${classInfo.classComment} 业务实现类
 *
 * Created by code generator on '${.now?string('yyyy-MM-dd HH:mm:ss')}'.
 */
@Service
public class ${classInfo.className}ServiceImpl implements ${classInfo.className}Service {

    private final static QcLog LOGGER = LogUtil.getLogger(${classInfo.className}ServiceImpl.class);

	@Resource
	private ${classInfo.className}Mapper ${classInfo.className?uncap_first}Mapper;

	/**
	 * 新增
	 */
	@Override
	public Response<${classInfo.fieldList[0].fieldClass}> insert(${classInfo.className}DO ${classInfo.className?uncap_first}DO) {

		// valid
		if (${classInfo.className?uncap_first}DO == null) {
			return Response.error(ErrorCode.PARAMETER_MISSING, "必要参数缺失");
		}

        ${classInfo.fieldList[0].fieldClass} id = ${classInfo.className?uncap_first}Mapper.insert(${classInfo.className?uncap_first}DO);
		return Response.success(id);
	}

	/**
	 * 删除
	 */
	@Override
	public boolean delete(${classInfo.fieldList[0].fieldClass} id) {
		int ret = ${classInfo.className?uncap_first}Mapper.delete(id);
		return ret>0;
	}

	/**
	 * 更新
	 */
	@Override
	public boolean update(${classInfo.className}DO ${classInfo.className?uncap_first}DO) {
		int ret = ${classInfo.className?uncap_first}Mapper.update(${classInfo.className?uncap_first}DO);
		return ret>0;
	}

	/**
	 * 查询
	 */
	@Override
	public ${classInfo.className}DO findById(${classInfo.fieldList[0].fieldClass} id) {
		return ${classInfo.className?uncap_first}Mapper.findById(id);
	}

	/**
	 * 分页查询
	 */
	@Override
	public Map<String,Object> pageList(int offset, int pagesize) {

		List<${classInfo.className}DO> pageList = ${classInfo.className?uncap_first}Mapper.pageList(offset, pagesize);
		int totalCount = ${classInfo.className?uncap_first}Mapper.pageListCount(offset, pagesize);

		// result
		Map<String, Object> result = new HashMap<String, Object>();
        result.put("pageList", pageList);
        result.put("totalCount", totalCount);

		return result;
	}


	/**
     * 批量新增或修改
     */
	@Transactional(rollbackFor = Exception.class)
	@Override
	public Response<String> saveAll(List<${classInfo.className}DO> ${classInfo.className?uncap_first}DOs) {

		if (${classInfo.className?uncap_first}DOs == null || ${classInfo.className?uncap_first}DOs.size() == 0) {
			return Response.error(ErrorCode.PARAMETER_MISSING, "参数缺失");
        }

		int effectedRows = ${classInfo.className?uncap_first}Mapper.insertOrUpdateBatch(${classInfo.className?uncap_first}DOs);
        return effectedRows > 0 ? Response.success() : Response.error();
	}
}
